/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.cloud.nacos.registry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * @author xiaojing
 * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
 * wangyuan
 * 此类继承自AbstractAutoServiceRegistration继承ApplicationListener<WebServerInitializedEvent>
 * 1、AbstractAutoServiceRegistration是spring-cloud-commons提供的，主要实现register方法，register方法的父类会调用实现ServiceRegistry<R>接口的register方法
 * nacos中实现ServiceRegistry<R>接口的是NacosServiceRegistry类，所有最终会调用到NacosServiceRegistry.register方法。
 * 2、ApplicationListener<WebServerInitializedEvent>类是spring用来发布事件监听的，使用了观察者模式设计，WebServerInitializedEvent监听事件
 * 是由spring-boot中spring-boot-starter-web提供的重写AbstractApplicationContext中最后一步finishRefresh的方法，
 * springboot中如果添加spring-boot-starter-web依赖就会到ServletWebServerApplicationContext中finishRefresh里面添加了ServletWebServerInitializedEvent事件
 * ServletWebServerInitializedEvent继承自WebServerInitializedEvent。
 * ApplicationListener中onApplicationEvent为主要方法，它在监听对应的关注事件后就会执行
 */
public class NacosAutoServiceRegistration
		extends AbstractAutoServiceRegistration<Registration> {

	private static final Logger log = LoggerFactory
			.getLogger(NacosAutoServiceRegistration.class);

	/**
	 * nacos的配置信息
	 */
	private NacosRegistration registration;

	public NacosAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry,
			AutoServiceRegistrationProperties autoServiceRegistrationProperties,
			NacosRegistration registration) {
		super(serviceRegistry, autoServiceRegistrationProperties);
		this.registration = registration;
	}

	@Deprecated
	public void setPort(int port) {
		getPort().set(port);
	}

	@Override
	protected NacosRegistration getRegistration() {
		if (this.registration.getPort() < 0 && this.getPort().get() > 0) {
			this.registration.setPort(this.getPort().get());
		}
		Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");
		return this.registration;
	}

	@Override
	protected NacosRegistration getManagementRegistration() {
		return null;
	}

	/**
	 * 重要类
	 * 当监听到WebServerInitializedEvent事件后，运行ApplicationListener中onApplicationEvent，然后进入bind->start->register
	 */
	@Override
	protected void register() {
		//判断是否设置isRegisterEnabled，默认true
		//isRegisterEnabled:如果您只想订阅，但不想注册服务，请将其设置为false
		if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
			log.debug("Registration disabled.");
			return;
		}
		if (this.registration.getPort() < 0) {
			this.registration.setPort(getPort().get());
		}
		//进入NacosServiceRegistry.register
		super.register();
	}

	@Override
	protected void registerManagement() {
		if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
			return;
		}
		super.registerManagement();

	}

	@Override
	protected Object getConfiguration() {
		return this.registration.getNacosDiscoveryProperties();
	}

	@Override
	protected boolean isEnabled() {
		return this.registration.getNacosDiscoveryProperties().isRegisterEnabled();
	}

	@Override
	@SuppressWarnings("deprecation")
	protected String getAppName() {
		String appName = registration.getNacosDiscoveryProperties().getService();
		return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
	}

}
